<?php
/**
 * This file houses the Fixture class.
 *
 * Copyright (c) 2010, Kalkomey Enterprises, Inc.
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 * 
 * - Redistributions of source code must retain the above copyright notice, this list
 *   of conditions and the following disclaimer.
 * - Redistributions in binary form must reproduce the above copyright notice, this 
 *   list of conditions and the following disclaimer in the documentation and/or other
 *   materials provided with the distribution.
 * - Neither the name of the Kalkomey Enterprises, Inc. nor the names of its 
 *   contributors may be used to endorse or promote products derived from this 
 *   software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
 * SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * 
 * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
 * @copyright Copyright (c) 2010, Kalkomey Enterprises, Inc.
 * @link http://code.google.com/p/swerv
 * @package Framework
 * @subpackage Library
 */

/**
 * The Fixture class is used by unit tests to create fake tables and data in the test database.
 *
 * @package Framework
 * @subpackage Library
 */
class Fixture {

	/**
	 * A fixture object used as the source of data needed to create the database structure/data.
	 *
	 * @var object
	 */
	private $source;
	
	/**
	 * Object constructor.
	 *
	 * @param object $fixtureSourceObject A fixture object used as the source of data needed to create the database structure/data
	 *
	 * @return Fixture
	 */
	public function __construct($fixtureSourceObject) {
		$this->source = $fixtureSourceObject;
	}

	/**
	 * Returns a mysqli object for working with the database.
	 * 
	 * @uses $mysqlTest
	 *
	 * @return mysqli
	 */
	private function getMysqliObj() {
		global $mysqlTest;
		$mysqli = new mysqli($mysqlTest['host'], $mysqlTest['user'], $mysqlTest['pass'], $mysqlTest['name'], $mysqlTest['port']);
		return $mysqli;
	}

	/**
	 * Used to insert data into a new created test database table.
	 *
	 * @uses Fixture::getMysqliObj()
	 *
	 * @return void
	 */
	private function insertData() {
		if (is_array($this->source->data)) {
			$mysqli = $this->getMysqliObj();
			foreach($this->source->data as $record) {
				$keys = '';
				$values = '';
				foreach ($record as $key => $val) {
					$keys .= "`{$key}`, ";
					$values .= "'{$val}', ";
				}
				$keys = substr($keys, 0, strlen($keys) - 2);
				$values = substr($values, 0, strlen($values) - 2);
				$sql = "INSERT INTO `{$this->source->tableName}` ({$keys}) VALUES ({$values})";
				$mysqli->query($sql);
			}
			$mysqli->close();
		}
	}
	
	/**
	 * Used to create a new database table.
	 *
	 * @uses Fixture::getMysqliObj()
	 *
	 * @return void
	 */
	public function setup() {
		$mysqli = $this->getMysqliObj();
		$mysqli->query($this->source->createSql);
		$mysqli->close();
		$this->insertData();
	}

	/**
	 * Used to remove all of the data from the table, then add fresh data back in.
	 *
	 * @uses Fixture::getMysqliObj()
	 * @uses Fixture::insertData()
	 *
	 * @return void
	 */
	public function resetData() {
		$mysqli = $this->getMysqliObj();
		$mysqli->query("TRUNCATE TABLE `{$this->source->tableName}");
		$mysqli->close();
		$this->insertData();
	}
	
	/**
	 * Used to remove the test database table.
	 *
	 * @uses Fixture::getMysqliObj()
	 *
	 * @return void
	 */
	public function destroy() {
		$mysqli = $this->getMysqliObj();
		$mysqli->query("DROP TABLE IF EXISTS `{$this->source->tableName}");
		$mysqli->close();
	}
	
}